﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using MyBetImporter.DAL;

namespace MyBetImporter
{
    [DataContractAttribute(IsReference = true)]
    [Serializable()]
    public class BetObject
    {

        private int _matchId;
        private int _teamHomeScore;
        private int _teamAwayScore;

        [Key()]
        [DataMemberAttribute()]
        public int MatchId
        {
            get { return _matchId; }
            set { _matchId = value; }
        }

        [DataMemberAttribute()]
        public int TeamHomeScore
        {
            get { return _teamHomeScore; }
            set { _teamHomeScore = value; }
        }

        [DataMemberAttribute()]
        public int TeamAwayScore
        {
            get { return _teamAwayScore; }
            set { _teamAwayScore = value; }
        }

        /// <summary>
        /// Create object from database information
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static BetObject CopyFrom(UserBets bet)
        {
            BetObject betTgt = new BetObject();
            betTgt.MatchId = bet.MatchId;
            betTgt.TeamHomeScore = bet.ScoreHome;
            betTgt.TeamAwayScore = bet.ScoreAway;
            return betTgt;
        }

        public static IList<UserBets> GetAllBetsInDatabase(ParieurProDBEntities dbContext, int championshipId, int dayNumber, int seasonId)
        {

            var queryBets = (from bets in dbContext.UserBets
                             where (bets.Matchs.Events.Contests.ChampionshipId == championshipId) && (bets.Matchs.Events.DayNumber == dayNumber) && (bets.Matchs.Events.Contests.SeasonId == seasonId)
                             orderby bets.aspnet_Users.UserName
                             select bets);

            IList<UserBets> betList = queryBets.ToList();
            return betList;
        }

        public static IList<UserBets> GetBetsInDatabase(ParieurProDBEntities dbContext, Guid userId, int championshipId, int dayNumber, int seasonId)
        {

            var queryBets = (from bets in dbContext.UserBets
                             where (bets.Matchs.Events.Contests.ChampionshipId == championshipId) && (bets.Matchs.Events.DayNumber == dayNumber) && (bets.Matchs.Events.Contests.SeasonId == seasonId)  && (bets.UserId == userId)
                             select bets);

            IList<UserBets> betList = queryBets.ToList();
            return betList;
        }

        public static void UpdateBetPoint(ParieurProDBEntities dbContext, UserBets currentBet)
        {
            var queryBets = (from bets in dbContext.UserBets
                             where (bets.UserId == currentBet.UserId) && (bets.MatchId == currentBet.MatchId) 
                             select bets);
            IList<UserBets> existingBets = queryBets.ToList();
            existingBets[0].Point = currentBet.Point;
            //dbContext.SaveChanges();
        }

    }
}